<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        @click="isEdit = !isEdit"
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        v-for="(item, index) in channels"
        :key="index"
        @click="onMyChannel(item, index)"
        class="grid-item"
      >
        <van-icon
          v-show="isEdit && !fixedChannels.includes(item.id)"
          slot="icon"
          name="clear"
        ></van-icon>
        <span class="text" :class="{ active: index === active }" slot="text">{{
          item.name
        }}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        v-for="item in recommendList"
        :key="item.id"
        class="grid-item"
        icon="plus"
        :text="item.name"
        @click="addChannel(item)"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel
} from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/stroage'
export default {
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allList: [],
      myChannel: this.channels,
      fixedChannels: [0],
      isEdit: false
    }
  },
  computed: {
    ...mapState('user', ['token']),
    recommendList () {
      const arr = []
      this.allList.forEach((channel) => {
        const res = this.myChannel.find((item) => {
          return item.id === channel.id
        })
        if (!res) {
          arr.push(channel)
        }
      })
      return arr
    }
  },
  created () {
    this.loadgetAllChannels()
  },
  methods: {
    async loadgetAllChannels () {
      try {
        const res = await getAllChannels()
        this.allList = res.data.channels
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    async addChannel (channel) {
      this.myChannel.push(channel)
      // 数据持久化处理
      if (this.token) {
        try {
          // 已登录，把数据请求接口放到线上
          await addUserChannel({
            id: channel.id, // 频道ID
            seq: this.myChannel.length // 序号
          })
        } catch (err) {
          this.$toast('保存失败，请稍后重试')
        }
      } else {
        // 未登录，把数据存储到本地
        setItem('TOUTIAO_CHANNELS', JSON.stringify(this.myChannel))
      }
    },
    // 点击频道
    onMyChannel (channel, index) {
      if (this.isEdit) {
        // 执行删除频道
        if (this.fixedChannels.includes(channel.id)) return
        // 删除指定频道
        this.myChannel.splice(index, 1)
        if (index <= this.active) {
          this.$emit('updateChannel', this.active - 1, true)
        }
        this.deleteChannel(channel)
      } else {
        // 跳转并关闭弹窗
        // 子传父
        this.$emit('updateChannel', index, false)
      }
    },
    // 删除
    async deleteChannel (channel) {
      try {
        if (this.token) {
          // 已登录 更新到线上
          await deleteUserChannel(channel.id)
        } else {
          // 未登录 将数据更新到本地
          console.log(this.myChannel, 'myChannel')
          setItem('TOUTIAO_CHANNELS', JSON.stringify(this.myChannel))
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
